package com.djhu.sql;
/**
 * Created by zw on 2017/12/29.
 */

import com.djhu.es.core.annotation.LogicType;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
public class DefaultSqlGeneraterService implements SqlGeneraterService{
    @Override
    public String sql(String table, List<FieldEntity> fieldEntityList) {
        String sql = "select * from " + table + " where 1 = 1 and";
        for (FieldEntity fieldEntity:fieldEntityList){
            sql = sql +  " " +  fieldtoStr(fieldEntity) + "";
        }
        return sql;
    }

    private String fieldtoStr(FieldEntity fieldEntity){
        String where = " ";
        if (CollectionUtils.isEmpty(fieldEntity.getFieldEntityList())){
            where = fieldEntity.getSqlLogic() + " " + fieldEntity.getField() + " = '" + fieldEntity.getValue() + "'  ";
            return where;
        }else {
            where = where + "(";
            Stack<FieldEntity> stack = new Stack<>();
            stack.addAll(fieldEntity.getFieldEntityList());
            int index = 0;
            Collections.sort(stack, new Comparator<FieldEntity>() {
                @Override
                public int compare(FieldEntity o1, FieldEntity o2) {
                    return 0 - o1.getSqlLogic().compareTo(o2.getSqlLogic());
                }
            });
            while (!stack.isEmpty())
            {
                FieldEntity field = stack.pop();
                if (CollectionUtils.isEmpty(field.getFieldEntityList())){
                    if (index == 0){
                        where = where   + " " +  field.getField() + " = '" + field.getValue() + "'  ";
                    }else {
                        where = where + " "  + field.getSqlLogic() + " " + field.getField() + " = '" + field.getValue() + "'  ";
                    }
                    index++;
                }else {
                    if (index ==  0){
                        where = where + "" + fieldtoStr(field) + "";
                    }else {
                        where = where + field.getSqlLogic() + "" + fieldtoStr(field) + "";
                    }
                   index++;
                }
            }
            where  = where+ ")";
        }
        return where;
    }

    public <T> String sql(T obj,Class<T> tClass)
    {


        return null;
    }



    @Override
    public String sql(String table, List<String> results, List<FieldEntity> fieldEntityList) {
        return null;
    }

    @Override
    public String sql(String table, List<String> results, List<FieldEntity> fieldEntityList, String note) {
        return null;
    }

    public static void main(String[] args) {
        DefaultSqlGeneraterService defaultSqlGeneraterService = new DefaultSqlGeneraterService();
        FieldEntity fieldEntity1 = new FieldEntity();
        fieldEntity1.setField("source");
        fieldEntity1.setValue("input");
        fieldEntity1.setSqlLogic(SqlLogic.OR);


        FieldEntity fieldEntity2 = new FieldEntity();
        fieldEntity2.setField("status");
        fieldEntity2.setValue("1");


        FieldEntity fieldEntity3 = new FieldEntity();
        fieldEntity3.setField("fieldEntity3");
        fieldEntity3.setValue("fieldEntity3");
        fieldEntity3.setFieldEntityList(new ArrayList<FieldEntity>());

        FieldEntity fieldEntity4 = new FieldEntity();
        fieldEntity4.setField("data_version");
        fieldEntity4.setValue("1.0.0");


        FieldEntity fieldEntity5 = new FieldEntity();
        fieldEntity5.setField("input_user");
        fieldEntity5.setValue("zw");
        fieldEntity5.setSqlLogic(SqlLogic.OR);

        fieldEntity3.getFieldEntityList().add(fieldEntity4);
        fieldEntity3.getFieldEntityList().add(fieldEntity5);

        FieldEntity fieldEntity = new FieldEntity();
        fieldEntity.setFieldEntityList(new ArrayList<FieldEntity>());
        fieldEntity.getFieldEntityList().add(fieldEntity1);
        fieldEntity.getFieldEntityList().add(fieldEntity2);
        fieldEntity.getFieldEntityList().add(fieldEntity3);

       // sql(String table, List<FieldEntity> fieldEntityList)
        String table = "HIUP_META_ROUTER";
        List<FieldEntity> fieldEntities = new ArrayList<>();
        fieldEntities.add(fieldEntity);
        fieldEntities.add(fieldEntity1);
        fieldEntities.add(fieldEntity2);
        System.out.println(defaultSqlGeneraterService.sql (fieldEntity4,  FieldEntity.class));

    }
}